package com.sun.sangeng.mytest;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test1 {

    public static void main(String[] args) {
        //test1();
        test2();
    }

    private static void test2() {
        Person person1 = new Person("小张1", 19);
        Person person2 = new Person("小张2", 20);
        Person person3 = new Person("小张3", 21);
        Person person4 = new Person("小张3", 22);

        List<Person> personList = new ArrayList<>();
        personList.add(person1);
        personList.add(person2);
        personList.add(person3);
        personList.add(person4);
        Stream<Person> stream = personList.stream();
    }

    private static void test1() {

        Person person1 = new Person("小张1", 19);
        Person person2 = new Person("小张2", 20);
        Person person3 = new Person("小张3", 21);
        Person person4 = new Person("小张3", 22);

        List<Person> personList = new ArrayList<>();
        personList.add(person1);
        personList.add(person2);
        personList.add(person3);
        personList.add(person4);
        
        List<String> stringList = new ArrayList<>();
        stringList.add("haha");
        
        Iterator<Person> iterator = personList.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

        System.out.println("---");
        
        for(Iterator<Person> it = personList.iterator();it.hasNext();){
            System.out.println(it.next());
        }

        System.out.println("---");
        
        int count = 0;
        Iterator<Person> iterator1 = personList.iterator();
        while (iterator1.hasNext()){
            Person person = iterator1.next();
            if(person.getAge() > 20){
                count++;
            }
        }
        System.out.println("count: "+count);
        
        int count1 = 0;
        for(Iterator<Person> it2 = personList.iterator();it2.hasNext();){
            if(it2.next().getAge() > 20){
                count1++;
            }
        }
        System.out.println("count1: "+count1);
        
        long count2 = personList.stream().filter((Person person) -> {
            return person.getAge() > 20;
        }).count();
        System.out.println("count2: "+count2);

        long count3 = personList.stream().filter((Person person) -> person.getAge() > 20).count();
        System.out.println("count3: "+count3);
        
        long count4 = personList.stream().filter(p -> p.getAge() > 20).count();
        System.out.println("count4: "+count4);

        List<Person> collect = personList.stream().filter(p -> p.getAge() > 20).collect(Collectors.toList());
        collect.forEach(System.out::println);
    }
}
